#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py

import os
from pathlib import Path
import argparse

tree_str = ''


def generate_tree(pathname, n=0):
    pathname = Path(pathname)
    global tree_str
    if pathname.is_file():
        tree_str += '    |' * n + '-' * 4 + pathname.name + '\n'
    elif pathname.is_dir():
        tree_str += '    |' * n + '-' * 4 + \
                    str(pathname.relative_to(pathname.parent)) + '\\' + '\n'
        for cp in pathname.iterdir():
            generate_tree(cp, n + 1)

def demo():
    parser = argparse.ArgumentParser(description='目录树生成')
    parser.add_argument('directory', type=str, help='目录')
    args = parser.parse_args()
    directory = args.directory
    if os.path.isdir(directory):
        generate_tree(directory)
        print(tree_str)
    else:
        print("请输入有效的目录")


if __name__ == '__main__':
    # generate_tree('.') #输出当前目录的目录树，.号代表当前目录
    # print(tree_str)
    demo()
